import React, { useMemo } from 'react';
import { checker } from '@@/hw-auth/fns/authProvider.ts';
import {authMatchEnum} from '@@/hw-auth/modal.d'
import { useHwAuthControl } from '@@/hw-auth/components/AuthContext';
import { useHwSettingAuthControl } from '@@/hw-auth/components/SettingAuthContext';
import styles from '@@/hw-auth/components/index.less';
import { CloseOutlined, PlusSquareOutlined } from '@ant-design/icons';
interface CheckAuthComponentProps {
  auth?: string;
}
const HwSettingComponent: React.FC<CheckAuthComponentProps> = ({
  children,
  auth,
}) => {
  const { settingAuthChecker } = useHwSettingAuthControl();

  const checkResult =
    settingAuthChecker.check(auth, 'component') !== authMatchEnum.notPass;

  if (checkResult) {
    return (
      <div className={styles.close}>
        {children}
        <div className={styles.close_btn}>
          <CloseOutlined />
        </div>
      </div>
    );
  }

  return (
    <div className={styles.open}>
      <div className={styles.open_btn}>
        <PlusSquareOutlined />
      </div>
      {children}
    </div>
  );
};
const HwPageComponent: React.FC<CheckAuthComponentProps> = ({
  children,
  auth,
}) => {
  const { pageAuthChecker } = useHwAuthControl();

  const checkResult =
    pageAuthChecker.check(auth, 'component') !== authMatchEnum.notPass;

  return checkResult ? <>{children}</> : null;
};
export const HwCheckAuthComponent: React.FC<CheckAuthComponentProps> = ({
  children,
  auth,
}) => {
  const { settingAuthChecker } = useHwSettingAuthControl();

  const Component = useMemo(() => {
    return settingAuthChecker ? HwSettingComponent : HwPageComponent;
  }, [children]);

  return <Component auth={auth}>{children}</Component>;
};
